package org.mjdai.server.exception;

import org.mjdai.server.utils.ResponseService;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalException {

    /**
     * 读取数据库异常
     * @param e
     * @return
     */
    @ExceptionHandler(SQLException.class)
    public ResponseService mySqlException(SQLException e) {
        if (e instanceof SQLIntegrityConstraintViolationException) {
            return ResponseService.error("该数据有关联数据，操作失败！");
        }
        return ResponseService.error("数据库异常，操作失败！");
    }
}
